#pragma once

#include "MessageHeader.h"
#include "MessageProducer.h"
#include "MessageConsumer.h"
#include "RedisPool.h"
#include <gtest/gtest.h>
#include <string>
#include <memory>

namespace MessageFramework {
namespace Test {

/**
 * @brief 测试辅助工具类
 */
class TestHelpers {
public:
    /**
     * @brief 获取测试Redis连接池
     */
    static RedisPool* getTestRedisPool();

    /**
     * @brief 清理测试Redis数据
     */
    static void cleanupRedis(const std::string& streamPattern = "test_*");

    /**
     * @brief 创建测试用MessageHeader
     */
    static MessageHeader createTestHeader(
        const std::string& messageType = "TestMessage",
        const std::string& publisherId = "TestPublisher");

    /**
     * @brief 创建测试用消息体
     */
    static std::string createTestBody(const std::string& content = "test");

    /**
     * @brief 验证两个MessageHeader相等
     */
    static bool headersEqual(const MessageHeader& h1, const MessageHeader& h2);

    /**
     * @brief 生成随机字符串
     */
    static std::string randomString(size_t length);

    /**
     * @brief 等待条件满足（超时返回false）
     */
    static bool waitFor(std::function<bool()> condition,
                       int timeoutMs = 5000,
                       int intervalMs = 100);
};

/**
 * @brief 测试Fixture基类（提供Redis环境）
 */
class RedisTestBase : public ::testing::Test {
protected:
    void SetUp() override;
    void TearDown() override;

    RedisPool* redisPool;
    std::string testStreamPrefix;
};

/**
 * @brief Mock Handler用于测试
 */
class MockHandler : public IMessageHandler {
public:
    MockHandler(const std::string& messageType = "MockMessage");

    int handleMessage(const Message& message) override;

    std::string getMessageType() const override {
        return m_messageType;
    }

    std::string getName() const override {
        return "MockHandler";
    }

    // 测试用接口
    int getCallCount() const { return m_callCount; }
    Message getLastMessage() const { return m_lastMessage; }
    void reset() {
        m_callCount = 0;
        m_lastMessage = Message();
    }

private:
    std::string m_messageType;
    int m_callCount = 0;
    Message m_lastMessage;
};

} // namespace Test
} // namespace MessageFramework
